#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdlib.h>
#include "msgq.h"
#include "tools.h"

Ctos ctos;
Stoc stoc;

void unlock(void)
{
	char path[PATH_MAX];
	sprintf(path,"%s/%u",ACC_PATH,ctos.acc.id);
	pf("对账号进行解锁操作：acc path:%s\n",path);
	int fd = open(path,O_RDWR);
	Ctos lock;
	read(fd,&lock.acc,sizeof(lock.acc));

	if(ERROR == fd)
	{
		perror("unlock");
		strcpy(stoc.str,"账户或密码错误！");
	}
	if(lock.acc.errno<3)
	{
		sprintf(stoc.str,"账号未被锁定!");
	}
	else
	{	
		if(0==strcmp(lock.acc.card,ctos.acc.card))
		{	
			ctos.acc.errno=0;
			sprintf(stoc.str,"解锁成功");
		}
		else
		{
			sprintf(stoc.str,"解锁失败");			
		}	
	}
	write(fd,&lock.acc,sizeof(lock.acc));
	close(fd);
	// 返回解锁成功信息
	stoc_send(&stoc);
}

int main()
{
	init_msgq();
	for(;;)
	{
		ctos_recv(&ctos,TYPE_UNLOCK);
		stoc.type = ctos.pid;
		unlock();
	}
}
